/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vistas;

import Beans.Clientes;
import Controladores.ControladorCliente;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ethan Amador
 */
public class ClienteListar extends javax.swing.JFrame {

    /**
     * Creates new form ClienteListar
     */
    boolean restaurar = false;
    int fila;
    ControladorCliente cc = new ControladorCliente();
    DefaultTableModel modeloTabla = new DefaultTableModel();

    public ClienteListar() {

        initComponents();
        jTable1.setModel(modeloTabla);
        jTable1.setEnabled(false);
        //Adicionamos las columnas 
        modeloTabla.addColumn("IDENTIFICACION");
        modeloTabla.addColumn("NOMBRE");
        modeloTabla.addColumn("APELLIDO");
        modeloTabla.addColumn("TELEFONO");
        modeloTabla.addColumn("DIRECCION");
        modeloTabla.addColumn("EMAIL");

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTxtListarIdentificacion = new javax.swing.JTextField();
        JbListarIdentificacion = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTxtNombre = new javax.swing.JTextField();
        jTxtApellido = new javax.swing.JTextField();
        jBListarNomApel = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jBListarTodo = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jBRestaurar = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Listar Clientes");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Listar Por Identificacion"));

        JbListarIdentificacion.setText("Listar");
        JbListarIdentificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JbListarIdentificacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(JbListarIdentificacion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTxtListarIdentificacion, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtListarIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JbListarIdentificacion))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Listar Por Nombre Y Apellido"));

        jLabel1.setText("NOMBRE");

        jLabel2.setText("APELLIDO");

        jBListarNomApel.setText("Listar");
        jBListarNomApel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBListarNomApelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxtApellido)))
                .addGap(18, 18, 18)
                .addComponent(jBListarNomApel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTxtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jBListarNomApel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Listar Todo"));

        jBListarTodo.setText("Listar");
        jBListarTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBListarTodoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jBListarTodo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addComponent(jBListarTodo)
                .addContainerGap())
        );

        jTable1.setModel(modeloTabla);
        jScrollPane1.setViewportView(jTable1);

        jBRestaurar.setText("Restaurar");
        jBRestaurar.setEnabled(false);
        jBRestaurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRestaurarActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jBRestaurar)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 690, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBRestaurar)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBListarTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBListarTodoActionPerformed
        // TODO add your handling code here:
        //Bloqueamos Listar por Id 
        jPanel1.setEnabled(false);
        JbListarIdentificacion.setEnabled(false);
        jTxtListarIdentificacion.setEnabled(false);
        //Bloqueamos Listar Por Nombre y apellidos
        jPanel2.setEnabled(false);
        jTxtNombre.setEnabled(false);
        jTxtApellido.setEnabled(false);
        jBListarNomApel.setEnabled(false);
        jLabel1.setEnabled(false);
        jLabel2.setEnabled(false);

        for (Clientes c : cc.Listas()) {
            String[] fila = new String[]{c.getIdentificacion(), c.getNombre(), c.getApellido(), c.getTelefono(), c.getDireccion(),
                c.getEmail()};
            modeloTabla.addRow(fila);
        }
        jBListarTodo.setEnabled(false);
        this.jBRestaurar.setEnabled(true);
    }//GEN-LAST:event_jBListarTodoActionPerformed

    private void JbListarIdentificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JbListarIdentificacionActionPerformed
        // TODO add your handling code here:
        //Bloqueamos Listar Por Nombre y apellidos
        jPanel2.setEnabled(false);
        jTxtNombre.setEnabled(false);
        jTxtApellido.setEnabled(false);
        jBListarNomApel.setEnabled(false);
        jLabel1.setEnabled(false);
        jLabel2.setEnabled(false);
        //Bloqueamos Listar Todo
        jPanel3.setEnabled(false);
        jBListarTodo.setEnabled(false);

        boolean existe = false;
        for (Clientes c : cc.Listas()) {
            if (c.getIdentificacion().equals(jTxtListarIdentificacion.getText())) {
                existe = true;
                String[] fila = new String[]{c.getIdentificacion(), c.getNombre(), c.getApellido(), c.getTelefono(), c.getDireccion(),
                    c.getEmail()};
                modeloTabla.addRow(fila);
                this.jBRestaurar.setEnabled(true);
            }

        }

        JbListarIdentificacion.setEnabled(false);
        jTxtListarIdentificacion.setEnabled(false);
        
        if (existe == false || cc.Posicion() < 0) {
            JOptionPane.showMessageDialog(null, "ERROR AL LISTAR CLIENTES", "ERROR!", JOptionPane.ERROR_MESSAGE);

        }
        this.jBRestaurar.setEnabled(true);

    }//GEN-LAST:event_JbListarIdentificacionActionPerformed

    private void jBRestaurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRestaurarActionPerformed
        // TODO add your handling code here:
        this.retaurarTable();
        restaurar = true;
        this.retaurar();
    }//GEN-LAST:event_jBRestaurarActionPerformed

    private void jBListarNomApelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBListarNomApelActionPerformed
        // TODO add your handling code here:
        //Bloquemos Listar Todo
        jPanel3.setEnabled(false);
        jBListarTodo.setEnabled(false);
        //Bloquemos Listar por Id
        jPanel1.setEnabled(false);
        JbListarIdentificacion.setEnabled(false);
        jTxtListarIdentificacion.setEnabled(false);
        boolean exi = false;
        for (Clientes c : cc.Listas()) {
            if (c.getNombre().equals(jTxtNombre.getText()) || c.getApellido().equals(jTxtApellido.getText())) {
                exi = true;
                String[] fila = new String[]{c.getIdentificacion(), c.getNombre(), c.getApellido(), c.getTelefono(), c.getDireccion(),
                    c.getEmail()};
                modeloTabla.addRow(fila);
                this.jBRestaurar.setEnabled(true);
            }else{
                JOptionPane.showMessageDialog(null, "ERROR AL LISTAR CLIENTES", "ERROR!", JOptionPane.ERROR_MESSAGE);
            this.jBRestaurar.setEnabled(true);
            }
        }
        if (exi == false) {
            JOptionPane.showMessageDialog(null, "ERROR AL LISTAR CLIENTES", "ERROR!", JOptionPane.ERROR_MESSAGE);
            this.jBRestaurar.setEnabled(true);
        }

    }//GEN-LAST:event_jBListarNomApelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClienteListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClienteListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClienteListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClienteListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClienteListar().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JbListarIdentificacion;
    private javax.swing.JButton jBListarNomApel;
    private javax.swing.JButton jBListarTodo;
    private javax.swing.JButton jBRestaurar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTxtApellido;
    private javax.swing.JTextField jTxtListarIdentificacion;
    private javax.swing.JTextField jTxtNombre;
    // End of variables declaration//GEN-END:variables

    private void retaurar() {
        //Activamos Listar Por Nombre y apellidos
        jPanel2.setEnabled(true);
        jTxtNombre.setEnabled(true);
        jTxtApellido.setEnabled(true);
        jBListarNomApel.setEnabled(true);
        jLabel1.setEnabled(true);
        jLabel2.setEnabled(true);
        //Activamos Listar por Id 
        jPanel1.setEnabled(true);
        JbListarIdentificacion.setEnabled(true);
        jTxtListarIdentificacion.setEnabled(true);
        //Activamos Listar Todo
        jPanel3.setEnabled(true);
        jBListarTodo.setEnabled(true);

        //Limpiamos Los Campos
        jTxtListarIdentificacion.setText(null);
        jTxtApellido.setText(null);
        jTxtNombre.setText(null);
    }

    private void retaurarTable() {
       //trabajar sobre esto 

        int a = modeloTabla.getRowCount() - 1;
        fila = a;
        for (int i = a; i >= 0; i--) {
            modeloTabla.removeRow(i);
           
        }

    }

}
